package cc.web.vaaking.services;

import cc.web.vaaking.common.DB.SuperSessionFactory;
import cc.web.vaaking.common.LoginContext;
import cc.web.vaaking.common.LoginUser;
import cc.web.vaaking.common.core.StateMachine;
import cc.web.vaaking.entity.SysVacationRecord;
import cc.web.vaaking.mapper.MiscMapper;
import cc.web.vaaking.mapper.SysVacationRecordMapper;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;


public class SysVacationRecordService {

    public int deleteByPrimaryKey(Integer id) {
        try (SqlSession session = SuperSessionFactory.sqlSessionFactory.openSession()) {
            SysVacationRecordMapper sysUserMapper = session.getMapper(SysVacationRecordMapper.class);
            return sysUserMapper.deleteByPrimaryKey(id);
        }
    }

    public int insert(int userId, SysVacationRecord record) {
        try (SqlSession session = SuperSessionFactory.sqlSessionFactory.openSession(true)) {
            DateTime now = new DateTime();
            record.setApproveStartTime(now);
            record.setApproveEndTime(DateUtil.offset(now, DateField.HOUR, 24));
            record.setStatus(StateMachine.getInitStatus(LoginContext.getCurrentUser().getRoleId()));
            SysVacationRecordMapper sysUserMapper = session.getMapper(SysVacationRecordMapper.class);
            MiscMapper miscMapper = session.getMapper(MiscMapper.class);
            sysUserMapper.insert(record);
            miscMapper.addUserVactionRecord(userId, record.getId());
            return 1;
        }
    }

    public int insertSelective(SysVacationRecord record) {
        try (SqlSession session = SuperSessionFactory.sqlSessionFactory.openSession()) {
            SysVacationRecordMapper sysUserMapper = session.getMapper(SysVacationRecordMapper.class);
            return sysUserMapper.insertSelective(record);
        }
    }

    public SysVacationRecord selectByPrimaryKey(Integer id) {
        try (SqlSession session = SuperSessionFactory.sqlSessionFactory.openSession()) {
            SysVacationRecordMapper sysUserMapper = session.getMapper(SysVacationRecordMapper.class);
            return sysUserMapper.selectByPrimaryKey(id);
        }
    }

    public int updateByPrimaryKeySelective(SysVacationRecord record) {
        try (SqlSession session = SuperSessionFactory.sqlSessionFactory.openSession(true)) {
            SysVacationRecordMapper sysUserMapper = session.getMapper(SysVacationRecordMapper.class);
            return sysUserMapper.updateByPrimaryKeySelective(record);
        }
    }

    public int updateByPrimaryKey(SysVacationRecord record) {
        try (SqlSession session = SuperSessionFactory.sqlSessionFactory.openSession()) {
            SysVacationRecordMapper sysUserMapper = session.getMapper(SysVacationRecordMapper.class);
            return sysUserMapper.updateByPrimaryKey(record);
        }
    }


    public List<SysVacationRecord> selectAllByUserId(int userid) {
        try (SqlSession session = SuperSessionFactory.sqlSessionFactory.openSession()) {
            SysVacationRecordMapper sysUserMapper = session.getMapper(SysVacationRecordMapper.class);
            return sysUserMapper.selectAllByUserId(userid);
        }
    }

    public List<SysVacationRecord> selectAllStatus(String status) {
        try (SqlSession session = SuperSessionFactory.sqlSessionFactory.openSession()) {
            SysVacationRecordMapper sysUserMapper = session.getMapper(SysVacationRecordMapper.class);
            return sysUserMapper.selectAllByStatus(status);
        }
    }

    public int getStatusTaskNum(String status) {
        try (SqlSession session = SuperSessionFactory.sqlSessionFactory.openSession()) {
            SysVacationRecordMapper sysUserMapper = session.getMapper(SysVacationRecordMapper.class);
            return sysUserMapper.getStatusTaskNum(status);
        }
    }

}
